<?php 

class gasActions extends sfActions
{
	public function executeIndex(sfWebRequest $request){
		
	}
	
	
	/**
	 * Fetch raw data and store parsed data into DB
	 * @param sfWebRequest $request
	 */
	public function executeFetchGas(sfWebRequest $request)
	{
		$area = 'Chicago-Central';
		$gasApi = new gasApi('Chicago+-+Central', '24');
		
		try{
			$gasApi->fetchGasPrices();
		}catch(Exception $e){
			sfContext::getInstance()->getLogger()->info($e->getMessage());
		}
		$this->rows = $gasApi->getPrices();
		
// 		$batch_id = $gasApi->createBatch($area);
// 		$gasApi->saveGasPrices($batch_id);
	}

	/**
	 * List all gas station brands
	 * @param sfWebRequest $request
	 */
	public function executeViewGas(sfWebRequest $request)
	{
		$this->brands = Doctrine::getTable('Gas')
		->createQuery()
		->select('name')
		->groupBy('name')
		->setHydrationMode(Doctrine_Core::HYDRATE_ARRAY)
		->execute();
	}
	
	/**
	 * List gas station locations of a given brand
	 * Build location-prices hash
	 * @param sfWebRequest $request
	 */
	public function executeListLocations(sfWebRequest $request){
		$this->brand = $this->getRequestParameter('brand');

		$max_ids = Doctrine::getTable('Gas')
		->createQuery()
		->select('max(id)')
		->where('name=?', $this->brand)
		->groupBy('address')
		->setHydrationMode(Doctrine_Core::HYDRATE_SINGLE_SCALAR)
		->execute(); 
		
// 		var_dump($max_ids);
		
		$gas = Doctrine::getTable('Gas')
		->createQuery()
		->select('address, regular, midgrade, premium')
		->whereIn('id', $max_ids)
		->orderBy('address')
		->setHydrationMode(Doctrine_Core::HYDRATE_ARRAY)
		->execute();
		
		$this->gas = $gas;
		
// 		var_dump($gas);
		
		// Get all records for a given brand
// 		$gas = Doctrine::getTable('Gas')
// 		->createQuery()
// 		->select('address, regular, midgrade, premium')
// 		->where('name = ?', $this->brand)
// 		->setHydrationMode(Doctrine_Core::HYDRATE_ARRAY)
// 		->execute();
		
		// Loop through records and build location-prices hash
		// output: location1 => regular, midgrade, premium
		//		   location2 => regular, midgrade, premium
// 		$loc_pri_hash = array();
// 		foreach($gas as $g){
// 			if(!array_key_exists($g['address'], $loc_pri_hash)){
// 				$loc_pri_hash[$g['address']] = array('regular'=>$g['regular'], 'midgrade'=>$g['midgrade'], 'premium'=>$g['premium']);
// 			}else{
// 				$loc_pri_hash[$g['address']]['regular'] .= (is_null($g['regular'])?",0":",{$g['regular']}");
// 				$loc_pri_hash[$g['address']]['midgrade'] .= (is_null($g['midgrade'])?",0":",{$g['midgrade']}");
// 				$loc_pri_hash[$g['address']]['premium'] .= (is_null($g['premium'])?",0":",{$g['premium']}");
// 			}
// 		}
			
// 		$this->locations = array_keys($loc_pri_hash);
// 		$this->loc_pri_hash = $loc_pri_hash;
	}
	
	/**
	 * List all prices of a given location
	 * @param sfWebRequest $request
	 */
	public function executeListPrices(sfWebRequest $request){
		$brand = $this->getRequestParameter('brand');
		$address = $this->getRequestParameter('address');
		
		$prices = Doctrine::getTable('Gas')
		->createQuery()
		->select('regular, midgrade, premium, reported_date')
		->where('address = ?', $address)
		->orderBy('reported_date')
		->setHydrationMode(Doctrine_Core::HYDRATE_ARRAY)
		->execute();
		
		$regular = $midgrade = $premium = array();
		foreach($prices as $price){
			$time = explode(" ", $price['reported_date']);
// 			var_dump($time[0]);
			$timestamp = strtotime($time[0])*1000;
			$regular[] = "[$timestamp, {$price['regular']}]";
			$midgrade[] = "[$timestamp, {$price['midgrade']}]";
			$premium[] = "[$timestamp, {$price['premium']}]";
		}
		$this->regular = $regular;
		$this->midgrade = $midgrade;
		$this->premium = $premium;
		
		$this->brand = $brand;
		$this->address = $address;
	}
	
	
	
}